//
// Created by Administrator on 2017/9/27 0027.
//

#include <jni.h>
#include <string>
#include<map>
#include<string>
#include "huffman.h"
#include<set>

using namespace std;
RootNode *root = NULL;
list<CodeTable *> *table = NULL;

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_example_huffman_HuffmanCodingCore_nativeBuildTree2(JNIEnv *env, jobject instance,
                                                            jbyteArray buf_) {
    jbyte *buf = env->GetByteArrayElements(buf_, NULL);
    jsize size = env->GetArrayLength(buf_);
    // TODO

    env->ReleaseByteArrayElements(buf_, buf, 0);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_example_huffman_HuffmanCodingCore_nativeRelease(JNIEnv *env, jobject instance) {
    if (root != NULL) {
        delete root;
    }
    if (table != NULL) {

        delete table;
    }
    // TODO

}


extern "C"
JNIEXPORT void JNICALL
Java_com_example_huffman_HuffmanCodingCore_nativeBuildTree(JNIEnv *env, jobject instance,
                                                           jstring str_) {
    const char *str2 = env->GetStringUTFChars(str_, 0);
    root = buildTree(str2);
    table = makeTable(root);
    env->ReleaseStringUTFChars(str_, str2);
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_huffman_HuffmanCodingCore_nativeDecoding(JNIEnv *env, jobject instance,
                                                          jstring str_) {
    const char *str = env->GetStringUTFChars(str_, 0);
    string result = decoding(root, str);
    env->ReleaseStringUTFChars(str_, str);
    return env->NewStringUTF(result.c_str());
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_huffman_HuffmanCodingCore_nativeTranscoding(JNIEnv *env, jobject instance,
                                                             jstring str_) {
    const char *str = env->GetStringUTFChars(str_, 0);
    string result = transcoding(table, str);
    env->ReleaseStringUTFChars(str_, str);
    return env->NewStringUTF(result.c_str());
}
